package cn.edu.scau.dropship.ServiceImpl;

import cn.edu.scau.dropship.entity.Finance_check;
import cn.edu.scau.dropship.entity.Finance_checkExample;
import cn.edu.scau.dropship.entity.TraceKey;
import cn.edu.scau.dropship.mapper.Finance_checkMapper;
import cn.edu.scau.dropship.service.FinanceService;
import cn.edu.scau.dropship.service.TraceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class FinanceServiceImpl implements FinanceService {

    @Autowired
    private Finance_checkMapper finance_checkMapper;
    @Autowired
    private TraceService traceService;

    @Override
    public long countByExample(Finance_checkExample example) {
        return finance_checkMapper.countByExample(example);
    }

    @Override
    public int deleteByExample(Finance_checkExample example) {
        return finance_checkMapper.deleteByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return finance_checkMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Finance_check record) {
        return finance_checkMapper.insert(record);
    }

    @Override
    public int insertSelective(Finance_check record) {
        return finance_checkMapper.insertSelective(record);
    }

    @Override
    public List<Finance_check> selectByExample(Finance_checkExample example) {
        return finance_checkMapper.selectByExample(example);
    }

    @Override
    public Finance_check selectByPrimaryKey(Integer id) {
        return finance_checkMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByExampleSelective(Finance_check record, Finance_checkExample example) {
        return finance_checkMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int updateByExample(Finance_check record, Finance_checkExample example) {
        return finance_checkMapper.updateByExample(record, example);
    }

    @Override
    public int updateByPrimaryKeySelective(Finance_check record) {
        return finance_checkMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Finance_check record) {
        return finance_checkMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Finance_check> getlist() {
        Finance_checkExample finance_checkExample = new Finance_checkExample();
        Finance_checkExample.Criteria criteria = finance_checkExample.createCriteria();
        criteria.andIdIsNotNull();
        List<Finance_check> list = finance_checkMapper.selectByExample(finance_checkExample);
        return list;
    }

    @Override
    public List<Finance_check> selectByWid(Integer wid) {
        //结果list
        List<Finance_check> result=new ArrayList<>();

        //根据wid查trace表，得到fid
        List<TraceKey> tracelist=traceService.selectByWid(wid);

        //根据fid查finance表
        for(int i=0;i<tracelist.size();i++){
            Integer fid=tracelist.get(i).getFcId();

            Finance_checkExample finance_checkExample = new Finance_checkExample();
            Finance_checkExample.Criteria criteria = finance_checkExample.createCriteria();
            criteria.andIdEqualTo(fid);
            List<Finance_check> flist = finance_checkMapper.selectByExample(finance_checkExample);
            result.add(flist.get(0));
        }

        return result;
    }

    @Override
    public Finance_check selectByApplyTime(Date time) {
        Finance_checkExample finance_checkExample = new Finance_checkExample();
        Finance_checkExample.Criteria criteria = finance_checkExample.createCriteria();
        criteria.andApplyTimeEqualTo(time);
        List<Finance_check> flist = finance_checkMapper.selectByExample(finance_checkExample);
        if(flist.size()>0){
            return flist.get(0);
        }
        return null;
    }

    /*@Override
    public Finance_check selectLast() {
        Finance_checkExample finance_checkExample = new Finance_checkExample();
        Finance_checkExample.Criteria criteria = finance_checkExample.createCriteria();
        criteria.and
        List<Finance_check> flist = finance_checkMapper.selectByExample(finance_checkExample);
        if(flist.size()>0){
            return flist.get(0);
        }
        return null;
    }*/
}
